import { defineStore } from 'pinia'
import { useTreeTableType } from '../interface/useTreeTableType'
import { getUserListTypeDataList } from '@/data/treeData/treeType'

import type { ResApiTableListDetail, ResApiTableList, TableData } from '@/api/tableApi/tableApiType'
import { reqTableData } from '@/api/tableApi/tableApi'
const useTreeTable = defineStore('useTreeTable', {
  state: () =>
    <useTreeTableType>{
      tableList: [],
      // 页码
      page: 1,
      // 每页条数
      pagesize: 10,
      // 总条数
      total: 0,
      // ----------------------
      //  树形表格数据
      treeTable: [],
      // 存储树形作为筛选条件的数据
      filterList: {
        area: '',
        department: ''
      }
    },
  actions: {
    // 获取表格表格数据
    async getTableDataList() {
      const res = await reqTableData(this.page, this.pagesize)
      this.tableList = res.data.list
      this.page = res.data.page
      this.pagesize = res.data.pagesize
      this.total = res.data.total
      console.log('🚚...  表格请求回来的数据', res.data)
    },
    //点击树形查询表格数据
    getTreeTable(val: any) {
      this.tableList = val
    },
    // 筛选
    filterTable() {
      const res = this.treeTable.filter((item) => {
        console.log(
          '🚚...  筛选',
          this.filterList,
          item.area,
          this.filterList.area,
          item.department,
          this.filterList.department
        )
        if (item.area === this.filterList.area && item.department === this.filterList.department) {
          return item
        }
      })
      this.treeTable = res
      console.log('🚚...  res', res)
    },
    // 获取筛选参数
    getFilterData(val: any, sub: any) {
      this.filterList.area = val
      this.filterList.department = sub
      this.filterTable()
    }
  },
  getters: {}
  // 持久化
  // persist: {
  //   enabled: true,
  //   strategies: [
  //     {
  //       // 测试表头数据
  //       key: 'tableHeader', // 存储的数据Key
  //       // storage: sessionStorage, //自定义存储到 sessionStorage 和 localStorage
  //       paths: ['tableHeader'] // 存储的数据
  //     }
  //     // ....
  //   ]
  // }
})

export default useTreeTable
